<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="dialogFormVisible = true">
        添加权限组
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      row-key="id"
      border
    >
      <el-table-column
        prop="roleName"
        label="权限组名称"
      />
      <el-table-column
        prop="roleCode"
        label="权限组标识"
      />
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="{row}">
          <el-button type="danger" @click="deleteItem(row.id)">删除权限组</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" style="width: 1100px;margin: auto">
      <RoleList
        @selectedRole="selectedRole"
      />
    </el-dialog>
  </div>
</template>
<script>
import { GetSystemRoleGroupItemList, SaveSystemRoleGroupItem, DeleteSystemRoleGroupItem } from '@/api/SystemManagerApi'
import RoleList from '@/views/SystemSetting/SystemRoleGroup/components/RoleList'
export default {
  components: { RoleList },
  props: ['roleGroupId'],
  data() {
    return {
      title: '选择权限组',
      dialogFormVisible: false,
      listLoading: false,
      list: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      GetSystemRoleGroupItemList(this.roleGroupId).then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    selectedRole(role) {
      this.$nextTick(() => {
        this.dialogFormVisible = false
      })

      SaveSystemRoleGroupItem({ roleGroupId: this.roleGroupId, roleId: role.id }).then((response) => {
        this.dialogFormVisible = false
        this.$notify({
          title: 'Success',
          message: 'Created Successfully',
          type: 'success',
          duration: 2000
        })
        this.getList()
      })
    },
    deleteItem(id) {
      this.listLoading = true
      DeleteSystemRoleGroupItem(id).then(() => {
        this.$nextTick(() => {
          this.listLoading = true
          this.getList()
        })
      })
    },
    uuid() {
      function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
      }
      return (S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4())
    }
  }
}
</script>
